package com.atgugu.flink.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/**
 * @Author lzc
 * @Date 2022/4/6 14:19
 */
public class Flink02_Operator_UnionList {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        env.enableCheckpointing(3000);  // 每3s把状态做做一次快照
        
        
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new MyFlatMapFunction())
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class MyFlatMapFunction implements FlatMapFunction<String, String>, CheckpointedFunction {
        // 每个并行度有一个自己的集合
        ArrayList<String> words = new ArrayList<>();
        private ListState<String> wordsState;
    
        @Override
        public void flatMap(String line, Collector<String> out) throws Exception {
    
            if (line.contains("x")) {
                throw new RuntimeException("程序异常退出...");
            }
            /*
            来一行字符串, 切成单词, 把这些单词保存到ArrayList
             */
            for (String word : line.split(" ")) {
                words.add(word);
            }
            out.collect(words.toString());
        }
        
        // 对状态进行快照: 数据以及存储到状态中, flink然后会自动对状态进行保存
        @Override
        public void snapshotState(FunctionSnapshotContext ctx) throws Exception {
            // 周期性的执行. 每隔一个周期, 每个并行度执行一次
//            System.out.println("MyFlatMapFunction.snapshotState");
            //wordsState.clear();
            /*for (String word : words) {
                
                wordsState.add(word);
            }*/
            
            // 用集合中的元素去替换状态中所有的元素
            wordsState.update(words);
            
        
        }
        
        // 初始化状态, 当程序启动的时候执行: 从状态中恢复需要数据
        @Override
        public void initializeState(FunctionInitializationContext ctx) throws Exception {
            //启动的时候执行. 每个并行度一致性一次
            System.out.println("MyFlatMapFunction.initializeState");
    
            wordsState = ctx.getOperatorStateStore()
                // 获取联合列表状态
                .getUnionListState(new ListStateDescriptor<String>("wordsState", String.class));
            // 从状态中回复数据到 ArrayList集合中 TODO
    
            Iterable<String> it = wordsState.get();
    
            for (String word : it) {
    
                words.add(word);
            }
        }
    }
}
